package org.ysh.vo;

import lombok.Data;
import org.ysh.domain.User;
import org.ysh.domain.UserRole;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Layui 穿梭框组件 VO
 */
@Data
public class TransferVo {

    // 主键 ==> ID
    private String value;

    // 显示值 ==> NAME
    private String title;

    // 是否选中
    private Boolean checked;

    /**
     * 将User列表转换为穿梭框组件
     * @param userRoles
     * @param users
     * @return
     */
    public static List<TransferVo> getTransferVos(List<UserRole> userRoles,List<User> users) {
        List<TransferVo>  transferVos = new ArrayList<>();
        users.forEach(user -> {
            TransferVo transferVo = new TransferVo();
            transferVo.setValue(user.getId().toString());
            transferVo.setTitle(user.getUsername());
            transferVos.add(transferVo);
        });
        List<Integer> ids = userRoles.stream().map(UserRole::getUserId).collect(Collectors.toList());
        transferVos.forEach(transferVo -> {
            if (ids.contains(Integer.parseInt(transferVo.value))) {
                transferVo.setChecked(true);
            }
        });
        return transferVos;
    }

}
